package com.lyf.scm.plan.api.controller;

import com.lyf.scm.plan.domain.service.RedisService;
import com.rome.arch.util.controller.RomeController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * redis服务器测试
 *
 * @author wangchlei
 * @date 2020/09/20
 */
@RequestMapping("/api/v1/redis")
@RomeController
public class RedisController {
    @Resource
    private RedisService redisService;

    /**
     * 获取所有键值对
     *
     * @param
     * @return {@link  List<Map<Object, Object>>}
     **/
    @GetMapping("/getAll")
    public List<Map<Object, Object>> getAllRedisVal1() {
        return redisService.getAllRedisVal1();
    }

    /**
     * 分页获取所有键值对
     *
     * @param
     * @return {@link  List<Map<Object, Object>>}
     **/
    @GetMapping("/getAllPage")
    public List<Map<Object, Object>> getAllRedisVal2(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return redisService.getAllRedisVal2(pageSize, pageNum);
    }

    /**
     * 根据key读取缓存
     *
     * @param key
     * @return {@link Object}
     **/
    @GetMapping("/get/{key}")
    public Object get(@PathVariable("key") String key) {
        return redisService.get(key);
    }

    /**
     * 写入key-value缓存
     *
     * @param key
     * @param value
     * @return {@link Object}
     **/
    @PostMapping("/set/{key}/{value}")
    public Object set(@PathVariable("key") String key, @PathVariable("value") String value) {
        return redisService.set(key, value);
    }

    /**
     * 设置 key--value 键值对（含有失效时长,秒为单位）
     *
     * @param key
     * @param value
     * @return {@link Object}
     **/
    @PostMapping("/set/{key}/{value}/{expireTime}")
    public Object set(@PathVariable("key") String key, @PathVariable("value") String value, @PathVariable("expireTime") Long expireTime) {
        return redisService.set(key, value, expireTime);
    }

    /**
     * 批量删除 key--value 键值对
     *
     * @param keys
     * @return {@link Object}
     **/
    @PostMapping("/batchDelete")
    public Object delete(@RequestBody String... keys) {
        return redisService.remove(keys);
    }

    /**
     * 获取key的失效时长（秒）
     *
     * @param key
     * @return {@link Object}
     **/
    @GetMapping("/expireTime/{key}")
    public Long getExpireTime(@PathVariable("key") String key) {
        return redisService.getExpireTime(key);
    }

    /**
     * 获取key的失效时长（秒）
     *
     * @param key
     * @return {@link Object}
     **/
    @GetMapping("/lRange/{key}/{start}/{end}")
    public List<Object> lRange(@PathVariable("key") String key, @PathVariable("start") Long start, @PathVariable("end") Long end) {
        return redisService.lRange(key, start, end);
    }

    /**
     * 根据key模糊删除
     *
     * @param key
     * @return {@link Object}
     **/
    @PostMapping("/remove")
    public Object batchRemove(@RequestParam("key") String key) {
        redisService.batchRemove(key);
        return "success";
    }

}
